package cn.schoolwow.quickflow.service.executor.option;

import cn.schoolwow.quickflow.QuickFlowExecutor;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.domain.FlowExecutorConfig;
import cn.schoolwow.quickflow.domain.FlowExecutorOption;
import cn.schoolwow.quickflow.domain.FlowExecutorRootConfig;
import cn.schoolwow.quickflow.util.QuickFlowUtil;
import com.alibaba.fastjson.JSONObject;

public class QuickFlowExecutorOptionOperationImpl implements QuickFlowExecutorOptionOperation{
    private FlowExecutorConfig flowExecutorConfig;

    public QuickFlowExecutorOptionOperationImpl(FlowExecutorConfig flowExecutorConfig) {
        this.flowExecutorConfig = flowExecutorConfig;
    }

    @Override
    public QuickFlowExecutor startRecord(boolean startRecord) {
        flowExecutorConfig.flowExecutorRootConfig.startRecord = startRecord;
        return flowExecutorConfig.quickFlowExecutor;
    }

    @Override
    public QuickFlowExecutor setFlowLogId(long flowLogId) {
        flowExecutorConfig.flowExecutorRootConfig.id = flowLogId;
        return flowExecutorConfig.quickFlowExecutor;
    }

    @Override
    public QuickFlowExecutor printTrace(boolean printTrace) {
        flowExecutorConfig.printTrace = printTrace;
        return flowExecutorConfig.quickFlowExecutor;
    }

    @Override
    public QuickFlowExecutor ignoreBroken(boolean ignoreBroken) {
        flowExecutorConfig.ignoreBroken = ignoreBroken;
        return flowExecutorConfig.quickFlowExecutor;
    }

    @Override
    public QuickFlowExecutor ignoreException(boolean ignoreException) {
        flowExecutorConfig.ignoreException = ignoreException;
        return flowExecutorConfig.quickFlowExecutor;
    }

    @Override
    public FlowExecutorOption generateFlowExecuteOption() {
        FlowExecutorOption flowExecutorOption = new FlowExecutorOption();
        flowExecutorOption.name = flowExecutorConfig.name;
        flowExecutorOption.requestData = new JSONObject(flowExecutorConfig.flowContext.getRequestDataMap());
        flowExecutorOption.flowArray = QuickFlowUtil.getFlowArray(flowExecutorConfig);
        return flowExecutorOption;
    }

    @Override
    public FlowContext getFlowContext() {
        return flowExecutorConfig.flowContext;
    }

    @Override
    public FlowExecutorConfig getFlowExecutorConfig() {
        return flowExecutorConfig;
    }

    @Override
    public FlowExecutorRootConfig getFlowExecutorRootConfig() {
        return flowExecutorConfig.flowExecutorRootConfig;
    }
}
